package com.ctn.information.data.retriever.impl.sample;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ctn.information.data.model.NewsTitle;
import com.ctn.information.data.retriever.Interface.NewsTitleRetriever;

/**
 * Sample Implementation for test only
 * @author Zale
 *
 */
public class NewsTitleRetrieverSampleImpl implements NewsTitleRetriever {
	private List<NewsTitle> titles;
	
	public NewsTitleRetrieverSampleImpl() {
		super();
		initDefaults();
	}

	private void initDefaults() {
		titles = new ArrayList<NewsTitle>();
		for(int i=1;i<20;i++){
			NewsTitle title = new NewsTitle();
			title.setName("titletitletitle"+i);
			title.setCreateDate(new Date());
			title.setMemo("infoinfoinfoinfo........."+i);
			titles.add(title);
		}
	}

	@Override
	public NewsTitle getById(Long id) {
		for(NewsTitle news:titles){
			if(news.getId()== id){
				return news;
			}
		}
		return null;
	}

	@Override
	public List<NewsTitle> getAll(boolean asc) {
		
		return titles;
	}

	@Override
	public List<NewsTitle> getDataByMenuId(Long id,int pageIndex,int count,boolean asc){
		List<NewsTitle> results = new ArrayList<NewsTitle>();
		for(NewsTitle news:titles){
			if(count>0){
				if(news.getId()>id){
					if(results.size()>count){
						break;
					}
					results.add(news);
				}
			}else{
				if(news.getId()<id){
					if(results.size()>-count){
						break;
					}
					results.add(news);
				}
			}
		}
		return results;
	
	}

	@Override
	public List<NewsTitle> getDataByQueryStr(String queryStr,int pageIndex,int count,boolean asc){
		return getDataByMenuId(0L, pageIndex, count, asc);
	}

}
